package com.enterprise.verifyformat;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
public class DateConverter extends DefaultTypeConverter {
    private static final DateFormat[] ACCEPT_DATE_FORMATS = {
            new SimpleDateFormat("dd/MM/yyyy"),
            new SimpleDateFormat("yyyy-MM-dd"),
            new SimpleDateFormat("yyyy/MM/dd") }; //支持转换的日期格式 
    @Override 
    public Object convertValue(Map context, Object value, Class toType) { 
        if (toType == Date.class) {  //浏览器向服务器提交时，进行String to Date的转换 
            Date date = null; 
            String dateString = null; 
            String[] params = (String[])value; 
            dateString = params[0];//获取日期的字符串
            System.out.println(dateString);
          
                    try {
                    	System.out.println(new SimpleDateFormat("yyyy-MM-dd").parse(dateString));
						return new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}//遍历日期支持格式，进行转换 
           
            } 
         
        else if (toType == String.class) {   //服务器向浏览器输出时，进行Date to String的类型转换 
            Date date = (Date)value; 
            System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
            return new SimpleDateFormat("yyyy-MM-dd").format(date);//输出的格式是yyyy-MM-dd 
        } 

        return null; 
    }
}